package com.atguigu.realtime.util;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author lzc
 * @Date 2023/3/21 11:39
 */
public class ThreadPoolUtil {
    public static ThreadPoolExecutor getThreadPool(){
        return new ThreadPoolExecutor(
            300, // 线程内核心线程数
            500, // 最大线程数
            120,  // 空闲线程的存活时间
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<>()
        );
    }
    
    public static void main(String[] args) {
        ThreadPoolExecutor threadPool = getThreadPool();
        System.out.println(threadPool.getTaskCount());
    
        // 给线程池提交任务
        threadPool.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("2开始执行:" + Thread.currentThread().getName());
    
                System.out.println("2:" +  threadPool.getTaskCount());
            }
        });
    
        threadPool.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("1开始执行:" + Thread.currentThread().getName());
                System.out.println("1:" +  threadPool.getTaskCount());
            }
        });
    }
}
